package com.xiaoshuidi.cloud.module.bpm.controller.app.reserveticket;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.bpm.controller.app.reserveticket.vo.CancelReserveTicketReqVO;
import com.xiaoshuidi.cloud.module.bpm.controller.app.reserveticket.vo.MyReserveTicketPageRespVO;
import com.xiaoshuidi.cloud.module.bpm.controller.app.reserveticket.vo.RemainingReserveQuantityReqVO;
import com.xiaoshuidi.cloud.module.bpm.controller.app.reserveticket.vo.ReserveCreateReqVO;
import com.xiaoshuidi.cloud.module.bpm.service.reserveticket.ReserveTicketService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.util.Map;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.module.bpm.enums.ErrorCodeConstants.MY_RESERVE_TICKET_ERROR;

@Tag(name = "租客app - 预约")
@RestController
@RequestMapping("/bpm/reserve")
@Validated
public class AppReserveController {
    @Resource
    private ReserveTicketService reserveTicketService;

    @PostMapping("/create")
    @Operation(summary = "租客创建预约工单")
    public CommonResult<Long> tenantReserveTicket(@Valid @RequestBody ReserveCreateReqVO createReqVO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        return success(reserveTicketService.tenantReserveTicket(userId, createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "租客端 - 我的预约")
    public CommonResult<PageResult<MyReserveTicketPageRespVO>> myReserveTicketPage(@RequestParam("mobile") String mobile,
                                                                                   @RequestParam("pageNo") Integer pageNo,
                                                                                   @RequestParam("pageSize") Integer pageSize) {
        if(!SecurityFrameworkUtils.getLoginUser().getMobile().equals(mobile)){
            throw new ServiceException(MY_RESERVE_TICKET_ERROR);
        }
        return success(reserveTicketService.myReserveTicketPage(mobile, pageNo, pageSize));
    }

    @PostMapping("/cancel-reserve-ticket")
    @Operation(summary = "取消预约工单")
    public CommonResult<Boolean> cancelReserveTicket(@Valid @RequestBody CancelReserveTicketReqVO cancelReserveTicketReqVO){
        String mobile = SecurityFrameworkUtils.getLoginUser().getMobile();
        return success(reserveTicketService.cancelReserveTicket(cancelReserveTicketReqVO.getReserveTicketId(), mobile));
    }

    @GetMapping("/remaining-reserve-quantity")
    @Operation(summary = "获取各时间段下可租数量")
    public CommonResult<Map<Integer, Integer>> remainingReserveQuantity(RemainingReserveQuantityReqVO reqVO){
        return success(reserveTicketService.remainingReserveQuantity(reqVO.getApartmentId(), reqVO.getReserveDate()));
    }

}
